package com.geetour.columbus.core.ibatis;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.geetour.columbus.common.exceptions.DAOException;
import com.geetour.columbus.common.model.MySqlTable;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * SqlMapClientDaoSupport extends class,<code>catch SQLException</code>
 * <code>throw DAOException</code>
 * 
 * @author ChenKai
 * @since 2012-4-22 2:17
 */
public class SqlMapClientDaoSupportEx extends SqlMapClientDaoSupport {

	public Long autoIncrement(String table) {
		MySqlTable mySqlTable = this.getMySqlTableInfo(table);
		if (mySqlTable != null) {
			return mySqlTable.getAuto_increment();
		} else {
			return null;
		}
	}

	public MySqlTable getMySqlTableInfo(String table) {
		if (table == null) {
			return null;
		}
		return (MySqlTable) this.queryObject("getMySqlTableInfo", table);
	}

	/**
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	public Object queryObject(String sqlID, Object obj) {
		try {
			return this.getSqlMapClient().queryForObject(sqlID, obj);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}

	/**
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	public List<?> queryList(String sqlID, Object obj) {
		try {
			return this.getSqlMapClient().queryForList(sqlID, obj);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}

	/**
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	public void insert(String sqlID, Object obj) {
		try {
			this.getSqlMapClient().insert(sqlID, obj);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}

	/**
	 * @param sqlID
	 * @param stList
	 * @return
	 * @throws DAOException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Integer insert(final String sqlID, final List<?> stList)
			throws DAOException {
		Integer result = Integer.valueOf(-1);
		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {

						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = Integer.valueOf(-1);
							executor.startBatch();
							for (int i = 0; i < stList.size(); i++) {
								executor.insert(sqlID, stList.get(i));
							}
							result = executor.executeBatch();
							return result;
						}
					});
		}
		return result;
	}

	/**
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	public int delete(String sqlID, Object obj) {
		try {
			return this.getSqlMapClient().delete(sqlID, obj);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}

	/**
	 * @param sqlID
	 * @param stList
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int delete(final String sqlID, final List<?> stList)
			throws DAOException {
		Integer result = Integer.valueOf(-1);
		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {

						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = Integer.valueOf(-1);
							executor.startBatch();

							for (int i = 0; i < stList.size(); i++) {
								executor.delete(sqlID, stList.get(i));
							}
							result = new Integer(executor.executeBatch());
							return result;
						}
					});
		}
		return result;
	}

	/**
	 * @param statementName
	 * @param parameterObject
	 * @return
	 * @throws DAOException
	 */
	public int update(String sqlID, Object obj) throws DAOException {
		try {
			return this.getSqlMapClient().update(sqlID, obj);
		} catch (SQLException e) {
			throw new DAOException(e);
		}
	}

	/**
	 * @param sqlID
	 * @param stList
	 * @return
	 * @throws DAOException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Integer update(final String sqlID, final List<?> stList)
			throws DAOException {
		Integer result = Integer.valueOf(-1);
		if ((sqlID != null) && (stList != null) && !stList.isEmpty()) {
			result = (Integer) getSqlMapClientTemplate().execute(
					new SqlMapClientCallback() {

						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							Integer result = Integer.valueOf(-1);
							executor.startBatch();

							for (int i = 0; i < stList.size(); i++) {
								executor.update(sqlID, stList.get(i));
							}
							result = new Integer(executor.executeBatch());
							return result;
						}
					});
		}
		return result;
	}
}
